package com.ding.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ding.eduservice.client.VodClient;
import com.ding.eduservice.entity.EduVideo;
import com.ding.eduservice.mapper.EduVideoMapper;
import com.ding.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-11-16
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;

    @Override
    public void updatevideodata(EduVideo eduVideo) {
        baseMapper.updateById(eduVideo);
    }

    @Override
    public void delvideodata(String videoId) {
       baseMapper.deleteById(videoId);
    }

    @Override
    public void addvideodata(EduVideo eduVideo) {
        baseMapper.insert(eduVideo);
    }

    @Override
    public EduVideo getInfoById(String videoId) {
        EduVideo eduVideo = baseMapper.selectById(videoId);
        return eduVideo;
    }

    @Override
    public void delvideoByCourseId(String courseId) {
        //根据课程id查询所有的视频id
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        wrapperVideo.select("video_source_id");
        List<EduVideo> eduCourses = baseMapper.selectList(wrapperVideo);//只能查询所有列



        ArrayList<String> videoIds = new ArrayList<>();
        for (int i = 0; i < eduCourses.size(); i++) { //只抽取出视频id这一列
            EduVideo eduVideo = eduCourses.get(i);
            String videoSourceId = eduVideo.getVideoSourceId();
            if(!StringUtils.isEmpty(videoSourceId)){
                videoIds.add(videoSourceId);
            }

        }

        if(videoIds.size()>0){
            vodClient.deleteBatch(videoIds);
        }


        baseMapper.deleteById(courseId);
    }

}
